package com.sky.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.sky.context.ThreadLocalUtil;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.ShoppingCart;
import com.sky.exception.BusinessException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    ShoppingCartMapper shoppingCartMapper;
    @Autowired
    DishMapper dishMapper;


    /**
     * 根据userid查询购物车列表
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        //获取userid
        Long userId = ThreadLocalUtil.getCurrentId();
        //根据userid查询购物车
        return shoppingCartMapper.getByUserId(userId);
    }

    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart=BeanUtil.copyProperties(shoppingCartDTO,ShoppingCart.class);
        //设置当前登录人的id
        shoppingCart.setUserId(ThreadLocalUtil.getCurrentId());
        //查询当前登录人菜品/套餐对应的购物车数据
        ShoppingCart cart=shoppingCartMapper.getOne(shoppingCart);
        //判断当前商品是否在购物车当中
        if (cart!=null){
            //如果有东西，就更新数量，数量+1
            cart.setNumber(cart.getNumber()+1);
            shoppingCartMapper.updateNumber(cart);
        }else {
            //如果不存在，插入数据，数量就是1
            Long dishId = shoppingCartDTO.getDishId();
            if (dishId!=null){
                Dish dish=dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }
            //无论是菜品还是套餐都需要设置数量和创建时间
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            //保存购物车
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     * 清空购物车
     */
    @Override
    public void clear() {
         shoppingCartMapper.clean(ThreadLocalUtil.getCurrentId());
    }

    @Override
    public void subtraction(ShoppingCartDTO shoppingCartDTO) {
        //DTO转PO
        ShoppingCart shoppingCartPO = BeanUtil.copyProperties(shoppingCartDTO, ShoppingCart.class);
        //根据PO条件查询购物车里的购物车数据
        shoppingCartPO.setUserId(ThreadLocalUtil.getCurrentId());
        ShoppingCart cart = shoppingCartMapper.getOne(shoppingCartPO);
        //购物车里没有商品
        if (cart==null){
            throw new BusinessException(500,"当前购物车未添加商品");
        }
        //购物车里添加了商品
        Integer number = cart.getNumber();
        if (number==1){
            //说明购物车有一个商品，直接删除记录
            shoppingCartMapper.deleteById(cart.getId());

        }else {
            //当前商品在购物车中的份数 > 1，修改份数即可
            cart.setNumber(cart.getNumber() - 1);
            shoppingCartMapper.updateNumber(cart);
        }

    }
}
